package com.ruoyi.project.naming.namesample.service.impl;

import com.ruoyi.common.utils.text.Convert;
import com.ruoyi.framework.aspectj.lang.annotation.DataSource;
import com.ruoyi.framework.aspectj.lang.enums.DataSourceType;
import com.ruoyi.project.naming.namebook.domain.NameBook;
import com.ruoyi.project.naming.namebook.mapper.NameBookMapper;
import com.ruoyi.project.naming.namesample.domain.NameSample;
import com.ruoyi.project.naming.namesample.mapper.NameSampleMapper;
import com.ruoyi.project.naming.namesample.service.INameSampleService;
import com.ruoyi.project.naming.namesentence.domain.NameSentence;
import com.ruoyi.project.naming.namesentence.mapper.NameSentenceMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 起名样例Service业务层处理
 *
 * @author shixiangwen
 * @date 2022-07-28
 */
@Service
@DataSource(DataSourceType.DICT)
public class NameSampleServiceImpl implements INameSampleService {
    @Autowired
    private NameSampleMapper nameSampleMapper;

    @Autowired
    private NameSentenceMapper nameSentenceMapper;

    @Autowired
    private NameBookMapper nameBookMapper;

    /**
     * 查询起名样例
     *
     * @param id 起名样例ID
     * @return 起名样例
     */
    @Override
    public NameSample selectNameSampleById(Long id) {
        return nameSampleMapper.selectNameSampleById(id);
    }


    public static String format(String sentence, String name) {
        for (int i = 0; i < name.length(); i++) {
            String zi = name.substring(i, i + 1);
            sentence = sentence.replaceFirst(zi, "<font color='red'>" + zi + "</font>");
        }
        return sentence;
    }

    /**
     * 查询起名样例列表
     *
     * @param nameSample 起名样例
     * @return 起名样例
     */
    @Override
    public List<NameSample> selectNameSampleList(NameSample nameSample) {
        List<NameSample> names = nameSampleMapper.selectNameSampleList(nameSample);
        names.forEach(item -> {
            NameSentence nameSentence = nameSentenceMapper.selectNameSentenceById(item.getSentenceId());
            if (nameSentence != null) {
                item.setSentence(format(nameSentence.getSentence(), item.getName()));
                NameBook nameBook = nameBookMapper.selectNameBookById(nameSentence.getBookId());
                if (nameBook != null) {
                    item.setBook(nameBook.getName());
                }
            }
        });
        return names;
    }

    /**
     * 新增起名样例
     *
     * @param nameSample 起名样例
     * @return 结果
     */
    @Override
    public int insertNameSample(NameSample nameSample) {
        return nameSampleMapper.insertNameSample(nameSample);
    }

    /**
     * 修改起名样例
     *
     * @param nameSample 起名样例
     * @return 结果
     */
    @Override
    public int updateNameSample(NameSample nameSample) {
        return nameSampleMapper.updateNameSample(nameSample);
    }

    /**
     * 删除起名样例对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteNameSampleByIds(String ids) {
        return nameSampleMapper.deleteNameSampleByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除起名样例信息
     *
     * @param id 起名样例ID
     * @return 结果
     */
    @Override
    public int deleteNameSampleById(Long id) {
        return nameSampleMapper.deleteNameSampleById(id);
    }
}
